package org.darkforest.controller;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import org.darkforest.auth.AuthPassport;
import org.darkforest.auth.CurrentMember;
import org.darkforest.dao.mapper.AddressMapper;
import org.darkforest.dao.mapper.AreaMapper;
import org.darkforest.model.*;
import org.darkforest.ucenter.service.AddressService;
import org.darkforest.ucenter.service.AreaRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Created by pc on 2017/10/16.
 */
@RestController
@RequestMapping(value = "address")
@Api(value = "address", description = "收货地址",produces=MediaType.APPLICATION_JSON_VALUE)
public class AddressController {
    @Autowired
    private AddressService addressService;
    @Autowired
    private AreaRedisService areaRedisService;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private AreaMapper areaMapper;

    @RequestMapping(value = "addressList",method = RequestMethod.GET)
    @ApiOperation(value = "我的收货地址列表", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    @AuthPassport
    public RestEntity addressList(@CurrentMember Member member) {
        try{
            return RestEntity.success(addressService.getAddressList(member.getMemberId()));
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    @RequestMapping(value = "areaList",method = RequestMethod.GET)
    @ApiOperation(value = "区域列表", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity getAddressAreaList(Integer paId) {
        try{
//            return RestEntity.success(areaRedisService.getAreaList(paId == null ? null : paId.toString()));
            List<Area> provinceList = areaRedisService.getAreaList(paId == null ? null : paId.toString());
                for(Area p:provinceList) {
                    List<Area> cityList = areaRedisService.getAreaList(p.getAreaId().toString());
                    p.setChildrenList(cityList);
                    for(Area c:cityList) {
                        List<Area> areaList = areaRedisService.getAreaList(c.getAreaId().toString());
                        c.setChildrenList(areaList);
                    }
                }
            return RestEntity.success(provinceList);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    /**
     * 保存新地址，返回保存成功的对象
     *
     * @return
     */
    @AuthPassport
    @RequestMapping(value = "newAddrAdd", method = RequestMethod.POST)
    @ApiOperation(value = "新增收货地址", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity newAddrAdd(@CurrentMember Member member, @RequestBody Address address) {
        try{
            address.setMemberId(member.getMemberId());
            if (address.getIsDefault() == null) {
                address.setIsDefault("0");
            }
            //如该用户没有任何保存地址，则直接设置为默认地址
            List hasAddr = addressMapper.selectByMember(address.getMemberId());
            if (hasAddr == null || hasAddr.size() == 0) {
                address.setIsDefault("1");
            }

            if(hasAddr.size()>0&&address.getIsDefault().equals("1")){
                AddressVo ads = new AddressVo();
                ads.setIsDefault("1");
                ads.setMemberId(member.getMemberId());
                addressService.addressEdit(ads);
            }

            /*******
             * 处理省市区名称展示
             */
            String regionName = this.areaName(address.getRegionId());
            String cityName = this.areaName(address.getCityId());
            String areaName = this.areaName(address.getAreaId());
            address.setRegionName(regionName);
            address.setCityName(cityName);
            address.setAreaName(areaName);
            address.setAreaInfo(regionName+" "+cityName+" "+ areaName);

            addressMapper.insertSelective(address);
            return RestEntity.success(address);
        }catch (Exception e){
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    @RequestMapping(value = "editAddr", method = RequestMethod.GET)
    @AuthPassport
    @ApiOperation(value = "根据ID获取收货地址", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity editAddress(@CurrentMember Member member, @RequestParam(required = true)Integer addressId) {
        try {
            AddressVo av = addressService.getAddressById(addressId, member.getMemberId());

            /*******
             * 处理省市区名称展示
             */
            String regionName = this.areaName(av.getRegionId());
            String cityName = this.areaName(av.getCityId());
            String areaName = this.areaName(av.getAreaId());
            av.setRegionName(regionName);
            av.setCityName(cityName);
            av.setAreaName(areaName);

            return RestEntity.success(av);
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    @RequestMapping(value = "delAddr", method = RequestMethod.GET)
    @AuthPassport
    @ApiOperation(value = "根据ID删除收货地址", httpMethod = "GET",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity delAddr(@CurrentMember Member member,  @RequestParam(required = true)Integer addressId) {
        try {
            addressService.delAddressById(addressId, member.getMemberId());
            return RestEntity.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed();
        }
    }

    @RequestMapping(value = "editAddrUpdate", method = RequestMethod.POST)
    @AuthPassport
    @ApiOperation(value = "修改收货地址", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity editAddrUpdate(@CurrentMember Member member, @RequestBody AddressVo vo) {
        try {
            if (vo.getIsDefault() == null) {
                vo.setIsDefault("0");
            }
            vo.setMemberId(member.getMemberId());

            List hasAddr = addressMapper.selectByMember(member.getMemberId());
            if(hasAddr.size()>0&&vo.getIsDefault().equals("1")){
                AddressVo ads = new AddressVo();
                ads.setIsDefault("1");
                ads.setMemberId(member.getMemberId());
                addressService.addressEdit(ads);
            }

            /*******
             * 处理省市区名称展示
             */
            String regionName = this.areaName(vo.getRegionId());
            String cityName = this.areaName(vo.getCityId());
            String areaName = this.areaName(vo.getAreaId());
            vo.setRegionName(regionName);
            vo.setCityName(cityName);
            vo.setAreaName(areaName);
            vo.setAreaInfo(regionName+" "+cityName+" "+ areaName);


            addressService.addressEdit(vo);
            return RestEntity.success(vo);
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed();
        }

    }

    @RequestMapping(value = "setDefaultAddress", method = RequestMethod.POST)
    @AuthPassport
    @ApiOperation(value = "设置默认收货地址", httpMethod = "POST",produces= MediaType.APPLICATION_JSON_VALUE)
    public RestEntity setDefaultAddress(@CurrentMember Member member, @RequestBody AddressVo address) {
        try {
            address.setIsDefault("1");
            address.setMemberId(member.getMemberId());
            addressService.addressEdit(address);
            return RestEntity.success("设置成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RestEntity.failed();
        }
    }


    public String areaName(Integer areaId){
        if(areaId!=null){
            Area area = areaMapper.selectByPrimaryKey(areaId);
            return area!=null?area.getAreaName():"";
        }
        return "";
    }
}
